<?php
	if ((($_FILES["file"]["type"] == "image/gif")
	|| ($_FILES["file"]["type"] == "image/jpeg")
	|| ($_FILES["file"]["type"] == "image/jpg")
	|| ($_FILES["file"]["type"] == "image/pjpeg"))
	&& ($_FILES["file"]["size"] < 20000000))
	{
		if ($_FILES["file"]["error"] > 0)
		{
			echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
		}
		else
		{
			//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
			//echo "Type: " . $_FILES["file"]["type"] . "<br />";
			//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
			//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

			if (file_exists("../../img/gallery/" . $_FILES["file"]["name"]))
			{
				echo $_FILES["file"]["name"] . " already exists. ";
			}
			else
			{
				move_uploaded_file($_FILES["file"]["tmp_name"], "../../img/gallery/" . $_FILES["file"]["name"]);
				//echo "Stored in: " . "../../img/gallery/" . $_FILES["file"]["name"];
				attachToDb();
			}
			}
	}
	else
	{
		echo "Invalid file<br>";
		echo $_FILES["file"]["size"];
	}
	
	
	
	function attachToDb(){
		include("../connect.php");
		//$query = "INSERT INTO images (filename, listorder, title, description) VALUES ('æøå', 117, 'Æblemandens ø', 'ærgerligt med æø og å!')";
		//mysql_query($query);
		
		//echo "Her er postværdierne: ".$_POST["uploadTitle"]." / ".$_POST["uploadDescription"];
		
		
		// Hent største listorder id
		$query = "SELECT MAX(listorder) as maxListOrder FROM images";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result) or die(mysql_error());
		$newListOrderId = $row['maxListOrder'] + 1;
		//echo "Højeste sorteringsnummer er: ".$newListOrd
		$query = "INSERT INTO images (filename, listorder, title, description) VALUES ('".$_FILES["file"]["name"]."', ".$newListOrderId.", '".$_POST["uploadTitle"]."', '".$_POST["uploadDescription"]."')";
		mysql_query($query);
		
		header("Location: ../index.php"); /* Redirect browser */
		
		
	}
?>